﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using Nvigorate.Common;
using Nvigorate.Common.Reflection;

namespace Nvigorate.Generation
{
    public class ResourceGeneration
    {
        public static void CreateResourceFile(string file, List<string> entries)
        {
            var writer = new ResXResourceWriter(file);
            entries.ForEach(p => writer.AddResource(p, p));
            writer.Generate();
        }

        public static void GenerateResourceFromClass(string typeName, string assemblyPath, string resourcePath)
        {
            var assembly = Assembly.LoadFrom(assemblyPath);
            var type = assembly.GetType(typeName);
            GenerateResourceFromClass(type, resourcePath);
        }

        public static void GenerateResourceFromClass(Type type, string resourcePath)
        {
            var properties = Reflector.GetProperties(type);
            CreateResourceFile(
                Path.Combine(resourcePath, type.Name + "Class.resx"),
                properties);
        }

        public static void GenerateResourcesForNamespace(string assemblyPath, string nameSpace, string resourcePath)
        {
            var assembly = Assembly.LoadFrom(assemblyPath);
            var module = assembly.GetModule(nameSpace);
            var typeList = module.GetTypes().ToList();
            typeList.ForEach(t => GenerateResourceFromClass(t, resourcePath));
        }

        public static void GenerateResourcesForNamespace(string assemblyPath, string resourcePath, params string[] types)
        {
            var assembly = Assembly.LoadFrom(assemblyPath);
            types
                .Select(t => assembly.GetType(t))
                .ToList()
                .ForEach(t => GenerateResourceFromClass(t, resourcePath));
        }

        public static void GenerateResourceFromTable(string connectionString, string tableName)
        {
            
        }
    }
}
